package com.haikesi.api.voicecontrol.service.impl;

import com.haikesi.api.base.Constants;
import com.haikesi.api.base.Error;
import com.haikesi.api.base.ErrorTools;
import com.haikesi.api.base.Type;
import com.haikesi.api.mode.domain.po.ModePO;
import com.haikesi.api.mode.service.IModeService;
import com.haikesi.api.subscribertable.domain.po.SubscriberTablePo;
import com.haikesi.api.voicecontrol.dao.VoiceControlDao;
import com.haikesi.api.voicecontrol.domain.po.VoiceControlPO;
import com.haikesi.api.voicecontrol.domain.vo.VoiceControlAddVO;
import com.haikesi.api.voicecontrol.domain.vo.VoiceControlSearchVO;
import com.haikesi.api.voicecontrol.domain.vo.VoiceControlUpdateVO;
import com.haikesi.api.voicecontrol.domain.vo.VoiceControlVO;
import com.haikesi.api.voicecontrol.service.IVoiceControlService;
import com.haikesi.exception.BusinessException;
import com.haikesi.util.PinYinUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class VoiceControlService implements IVoiceControlService {
    @Autowired
    private  VoiceControlDao voiceControlDao;
    @Autowired
    private IModeService modeService;

    @Override
    public List<VoiceControlVO> getList(VoiceControlSearchVO searchVO) {
        VoiceControlPO po = new VoiceControlPO();
        if (po.getUser() == null) {
            searchVO.setOrganizationId("");
            return voiceControlDao.getList(searchVO);
        }
        searchVO.setOrganizationId(po.getUser().getOrganizationId());
        if (po.getUser().getIsAdmin() == 1) {
            searchVO.setOrganizationId("");
        }
        return voiceControlDao.getList(searchVO);
    }

    @Override
    public void update(VoiceControlUpdateVO vo) {
        VoiceControlPO po = new VoiceControlPO();
        BeanUtils.copyProperties(vo, po);
        Example example = new Example(VoiceControlPO.class);
        String contentPinYin= PinYinUtil.toPinyin(vo.getVoiceContent());
        example.and().andEqualTo("voiceContentPinyin",contentPinYin).andNotEqualTo("id",vo.getId());
        if(po.getUser()!=null){
            example.and().andEqualTo("organizationId", po.getUser().getOrganizationId());
        }
        List<VoiceControlPO> list = voiceControlDao.selectByExample(example);
        if (list.size()>0) {
            throw new BusinessException(Type.EXIST_ERROR, ErrorTools.ErrorAsArrayList(new Error("subscriberNumber", "音频内容已经存在")));
        }
        po.preUpdate();
        po.setVoiceContentPinyin(contentPinYin);
        int i = voiceControlDao.updateByPrimaryKeySelective(po);
        if (i == Constants.SQL_RETUN_SUCCESS) {
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }
    }

    @Override
    public void add(VoiceControlAddVO vo) {
        VoiceControlPO po = new VoiceControlPO();
        Example example = new Example(VoiceControlPO.class);
        String contentPinYin= PinYinUtil.toPinyin(vo.getVoiceContent());
        example.and().andEqualTo("voiceContentPinyin",contentPinYin);
        if(po.getUser()!=null){
            example.and().andEqualTo("organizationId", po.getUser().getOrganizationId());
        }
        List<VoiceControlPO> list = voiceControlDao.selectByExample(example);
        if (list.size()>0) {
            throw new BusinessException(Type.EXIST_ERROR, ErrorTools.ErrorAsArrayList(new Error("subscriberNumber", "音频内容已经存在")));
        }
        BeanUtils.copyProperties(vo, po);
        po.preInsert();
        if(po.getUser()!=null){
            po.setOrganizationId(po.getUser().getOrganizationId());
        }
        po.setVoiceContentPinyin(contentPinYin);
        int i = voiceControlDao.insertSelective(po);
        if (i == Constants.SQL_RETUN_SUCCESS) {
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }
    }

    @Override
    public void delete(String id) {
        VoiceControlPO po = new VoiceControlPO();
        po.setId(id);
        po.preDelete();
        int i = voiceControlDao.insertSelective(po);
        if (i == Constants.SQL_RETUN_SUCCESS) {
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }
    }

    @Override
    public void sendVocie(String text) {
        VoiceControlPO po = new VoiceControlPO();
        Example example = new Example(VoiceControlPO.class);
        example.and().andEqualTo("voiceContentPinyin",PinYinUtil.toPinyin(text));
        if(po.getUser()!=null){
            example.and().andEqualTo("organizationId", po.getUser().getOrganizationId());
        }
        VoiceControlPO vpo = voiceControlDao.selectOneByExample(example);
        if (vpo==null) {
            throw new BusinessException(Type.NOT_FOUND_ERROR, ErrorTools.ErrorAsArrayList(new Error("sendVocie", "没有指定的音频控制")));
        }
        modeService.controlMode(vpo.getVoiceModeId());
    }
}
